package com.goldsprite.gameframeworks.ecs.component;

import com.goldsprite.gameframeworks.ecs.*;
import com.goldsprite.gameframeworks.ecs.entity.*;
import com.goldsprite.gameframeworks.ecs.system.GameSystem;

	public interface IComponent extends IRunnable {
	ComponentField getCompFields();

	@Override
	default IRunnable.IRunnableFields getIRunnableFields() {
		return getCompFields();
	}

	default boolean isShowGizmos() {
		return getCompFields().showGizmos;
	}
	default void drawGizmos() {}

	default void initIComponent() {
		initIRunnable();
	}

	default boolean isAwake() {
		return getCompFields().awaked;
	}

	default boolean isEnable() {
		return getCompFields().enabled;
	}
	default void setEnable(boolean isEnable) {
		getCompFields().enabled = isEnable;
	}

	default boolean isDestroyed() {
		return getCompFields().destroyed;
	}

	default String getTag() {
		return getCompFields().tag;
	}
	default void setTag(String tag) {
		getCompFields().tag = tag;
	}

	default GObject getGObject() {
		return getCompFields().gobject;
	}
	default void setGObject(GObject gObject) {
		getCompFields().gobject = gObject;
		getCompFields().transform = gObject.transform;
	}

	default TransformComponent getTransform() {
		return getCompFields().transform;
	}

	default void preRegisterCompType() {
		Class type = getClass();
		ComponentManager.preRegisterComponentType(type);
	}

	default void awake(){
		if(isAwake()) return;

		getCompFields().awaked = true;
		// 自动注册到组件管理器
		if (getGObject() != null) {
			ComponentManager.registerComponent(getGObject(), this.getClass(), this);
			ComponentManager.updateEntityComponentMask(getGObject());
		}
	}


	@Override
	default void fixedUpdate(float fixedDelta){}

	@Override
	default void update(float delta) {}

	default <T extends IComponent> T getComponent() {
		Class<T> type = null;
		return getGObject().getComponent(type);
	}

	default <T extends IComponent> T getComponent(Class<T> type) {
		return getGObject().getComponent(type);
	}

	default <T extends IComponent> T getComponent(Class<T> type, int index) {
		return getGObject().getComponent(type, index);
	}

	default <T extends IComponent> T addComponent(Class<T> clazz) {
		return getGObject().addComponent(clazz);
	}

	default <T extends IComponent> T addComponent(T component) {
		return getGObject().addComponent(component);
	}

	default void destroy() {
		if (isDestroyed()) return;

		getCompFields().destroyed = true;
		GameSystem.getInstance().addDestroyComponent(this);
	}

	default void destroyImmediate() {
		if (getGObject() != null) {
			getGObject().removeComponent(this);
			// 从组件管理器注销
			ComponentManager.unregisterComponent(getGObject(), this.getClass(), this);
		}
	}

	public static class ComponentField extends IRunnableFields {
		public GObject gobject;
		public TransformComponent transform;
		protected boolean awaked = false;
		protected boolean enabled = true;
		protected boolean destroyed = false;
		protected boolean showGizmos = true;
		protected String tag;
	}

}
